package com.dk.shiro;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShrioConfig {
   //shiro是基于过滤器的
   //创建shiro的filterfactory,shiro已经实现，我们只需要将此类托管给ioc容器

    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Autowired DefaultWebSecurityManager securityManager){

        ShiroFilterFactoryBean shiroFilterFactoryBean=new ShiroFilterFactoryBean();
        //设置会话管理
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        //设置哪些资源放过，哪些资源拦截
        Map<String,String> filterMap =new LinkedHashMap<>();//保证存取有序

        /**
         * shiro 内置过滤器
         * anon: 无需认证
         * authc:认证之后才可以访问
         * perms：认证之后有权限可以访问
         */
        filterMap.put("/index","anon");//放过首页
        filterMap.put("/login","anon");//放过首页

        //设置必须登录才能访问，perms需要设置令牌
        filterMap.put("/add","perms[add]");
        filterMap.put("/update","perms[update]");
        filterMap.put("/delete","perms[delete]");
        filterMap.put("/download","perms[delexcel]");

        filterMap.put("/auth/out","logout");


        filterMap.put("/**","authc");//放过首页

        //修改登录跳转地址
        shiroFilterFactoryBean.setLoginUrl("/tologin");
        //设置为授权提示页面
        shiroFilterFactoryBean.setUnauthorizedUrl("/unauth");


        //将map存到shiro的过滤器中
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
        return shiroFilterFactoryBean;
    }

    /**
     * 创建SecurityManager
     */
    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Autowired UserRealm userRealm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //关联realm
        securityManager.setRealm(userRealm);
        return securityManager;
    }

    //配置realm类
    @Bean
    public UserRealm getUserRealm(@Autowired HashedCredentialsMatcher hashedCredentialsMatcher) {
        UserRealm userRealm = new UserRealm();
        //设置加密方式
        userRealm.setCredentialsMatcher(hashedCredentialsMatcher);
        return userRealm;
    }

    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher() {
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        //指定加密方式,sha-1 sha-2
        hashedCredentialsMatcher.setHashAlgorithmName("MD5");
        //加密次数 1234 - > xxxabc -> yyydef
        hashedCredentialsMatcher.setHashIterations(3);
        //加密的编码
        hashedCredentialsMatcher.setStoredCredentialsHexEncoded(true);
        return hashedCredentialsMatcher;
    }


    //shiro扩展标签库
    @Bean
    public ShiroDialect shiroDialect(){
        return new ShiroDialect();
    }
}
